//
//  PMSideStatisticsHomeChartCell.m
//  XTCloud
//
//  Created by QA on 2017/12/11.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "PMSideStatisticsHomeChartCell.h"
#import "Charts-Swift.h"
#import "GEObjectValueTableViewCell.h"
#import "NSDictionary+DataFormat.h"
#import "StringAxisValueFormatter.h"

@interface PMSideStatisticsHomeChartCell()

@end

@implementation PMSideStatisticsHomeChartCell

- (void)setArrayValue:(NSArray*)valueArray index:(NSInteger)index
{
    if(index==0){
        [self drawFFFLChartCell:valueArray];
    } else {
        [self drawYKFLChartCell:valueArray];
    }
}


#pragma mark - 发货量签收量图表

- (void)drawFFFLChartCell:(NSArray*)valueArray
{
    //绘制标题
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, SuperWidth - 40, 22)];
    titleLable.text = @"各站砼方量对比分析(方)";
    titleLable.font = [UIFont systemFontOfSize:16];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:titleLable];
    
    //无数据绘制提示
    if (valueArray.count <= 0) {
        //[cell drawNotDataViewTableView:self.tableView rect:CGRectMake(0, 60, SuperWidth, 180)];
        return;
    }
    
    BarChartView *_chartView = [[BarChartView alloc] initWithFrame:CGRectMake(0, 40, SuperWidth-10, 210.0)];
//    _chartView.descriptionText = @"";
    _chartView.noDataText = @"暂无数据";
    
    _chartView.pinchZoomEnabled = NO;
    _chartView.drawBarShadowEnabled = NO;
    _chartView.drawGridBackgroundEnabled = NO;
    
    ChartLegend *legend = _chartView.legend;
    legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
    legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
    legend.orientation = ChartLegendOrientationVertical;
    legend.drawInside = YES;
    legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:8.f];
    legend.yOffset = 10.0;
    legend.xOffset = 10.0;
    legend.yEntrySpace = 0.0;
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xAxis.labelPosition = XAxisLabelPositionBothSided;
    xAxis.granularity = 1.f;
    xAxis.centerAxisLabelsEnabled = YES;
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    leftAxis.drawGridLinesEnabled = NO;
    leftAxis.spaceTop = 0.35;
    leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
    
    //_chartView.rightAxis.enabled = YES;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.drawLabelsEnabled = NO;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.axisMinimum = 0.0;
    
    _chartView.data = [self generateBar:_chartView data:valueArray];
    [self.contentView addSubview:_chartView];
}

- (BarChartData *)generateBar:(BarChartView*)chartView data:(NSArray*)array
{
    NSInteger groupCount = array.count;
    float groupSpace = 0.1f;
    float barSpace = 0.05f;
    float barWidth = 0.4f;
    // (0.25 + 0.05) * 2 + 0.2 = 1.00 -> interval per "group"
    
    NSMutableArray *xVals = [NSMutableArray array];
    for (int j = 0; j < array.count; j++) {
        NSDictionary *mDictionary = [array objectAtIndex:j];
        NSString *clmc = [mDictionary objectFormatStringForKey:@"站点名称"];
        [xVals addObject:clmc];
    }
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *mDictionary = [array objectAtIndex:i];
        
        CGFloat weight1 = [mDictionary objectFormatFloatForKey:@"发货方量" retain:1 except:1];
        [yVals1 addObject:[[BarChartDataEntry alloc] initWithX:i y:weight1]];
        
        CGFloat weight2 = [mDictionary objectFormatFloatForKey:@"签收方量" retain:1 except:1];
        [yVals2 addObject:[[BarChartDataEntry alloc] initWithX:i y:weight2]];
        
    }
    //设置底部显示文字
    chartView.xAxis.granularity = 1.0; // only intervals
    chartView.xAxis.valueFormatter = [[StringAxisValueFormatter alloc] initWithData:xVals];
    
    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals1 label:@"发货方量"];
    [set1 setColor:ThemeColorGreen];
    BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithValues:yVals2 label:@"签收方量"];
    [set2 setColor:ThemeColorLigthBlue];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    [dataSets addObject:set2];
    
    BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    [data setDrawValues:NO];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    
    // specify the width each bar should have
    data.barWidth = barWidth;
    
    // restrict the x-axis range
    chartView.xAxis.axisMinimum = 0;
    
    // groupWidthWithGroupSpace(...) is a helper that calculates the width each group needs based on the provided parameters
    chartView.xAxis.axisMaximum = [data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * groupCount;
    
    [data groupBarsFromX:0 groupSpace:groupSpace barSpace:barSpace];
    
    return data;
}


#pragma mark - 绘制盈亏分析

- (void)drawYKFLChartCell:(NSArray*)valueArray
{
    //绘制标题
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, SuperWidth - 40, 22)];
    titleLable.text = @"各站盈亏方量分析(方)";
    titleLable.font = [UIFont systemFontOfSize:16];
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:titleLable];
    
    BarChartView *_chartView = [[BarChartView alloc] initWithFrame:CGRectMake(5, 30, SuperWidth-10, 230)];
//    _chartView.descriptionText = @"";
    
    _chartView.drawBarShadowEnabled = NO;
    _chartView.pinchZoomEnabled = NO;
    _chartView.drawGridBackgroundEnabled = NO;
    
    ChartXAxis *xAxis = _chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.drawLabelsEnabled = YES;
    
    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.spaceTop = 0.15;
    
    ChartYAxis *rightAxis = _chartView.rightAxis;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    rightAxis.spaceTop = 0.15;
    
    ChartLegend *l = _chartView.legend;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
    l.verticalAlignment = ChartLegendVerticalAlignmentBottom;
//    l.orientation = ChartLegendOrientationVertical;
    
    l.form = ChartLegendFormSquare;
    l.formSize = 9.0;
    l.font = [UIFont systemFontOfSize:11.f];
    l.xEntrySpace = 4.0;
    
    [self setMixtureRatioChartDataChart:_chartView data:valueArray];
    [self.contentView addSubview:_chartView];
    [_chartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setMixtureRatioChartDataChart:(BarChartView *)chartView data:(NSArray*)array
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    NSMutableArray *entries = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < array.count; i++)
    {
        NSDictionary *mDictionary = [array objectAtIndex:i];
        NSString *name = [mDictionary objectFormatStringForKey:@"站点名称" defaults:@"未知"];
        
        CGFloat weight1 = [mDictionary objectFormatFloatForKey:@"生产方量" retain:1 except:1];
        CGFloat weight2 = [mDictionary objectFormatFloatForKey:@"签收方量" retain:1 except:1];
        //（签收方量-生产方量>0）= 盈利
        CGFloat weight3 = weight2 - weight1;
        
        [xVals addObject:name];
        [entries addObject:[[BarChartDataEntry alloc] initWithX:i y:weight3]];
    }
    
    //设置底部显示文字
    chartView.xAxis.granularity = 1.0; // only intervals
    chartView.xAxis.valueFormatter = [[StringAxisValueFormatter alloc] initWithData:xVals];
    chartView.rightAxis.enabled = NO;
    
    BarChartDataSet *set = [[BarChartDataSet alloc] initWithValues:entries label:@"站点盈亏方量"];
    //set.drawIconsEnabled = NO;
    set.stackLabels = xVals;
    [set setColor:[UIColor colorWithRed:240/255.f green:120/255.f blue:124/255.f alpha:1.f]];
    
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set];
    
    BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
    //[data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:leftAxisFormatter]];
    [data setDrawValues:YES];
    data.barWidth = 0.5f;
    
    chartView.data = data;
}

@end
